package com.das.mechanic_base.gen;

import com.das.mechanic_base.bean.processsive.ProcessNewBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：zhanglei on 2019/11/16 10:38
 * <p>
 * 邮箱：916256628@qq.com
 */
public class ShortBeanConverter implements PropertyConverter<List<ProcessNewBean.ServiceBaseShortBean>, String> {
    @Override
    public List<ProcessNewBean.ServiceBaseShortBean> convertToEntityProperty(String databaseValue) {
        if (databaseValue == null) {
            return null;
        } else {
            JsonArray jsonArray = new JsonParser().parse(databaseValue).getAsJsonArray();
            Gson gson = new Gson();
            List<ProcessNewBean.ServiceBaseShortBean> recordKeyBeans = new ArrayList<>();
            //循环遍历
            for (JsonElement user : jsonArray) {
                //通过反射 RecordKeyBean.class
                ProcessNewBean.ServiceBaseShortBean keyBean = gson.fromJson(user, ProcessNewBean.ServiceBaseShortBean.class);
                recordKeyBeans.add(keyBean);
            }
            return recordKeyBeans;
        }
    }

    @Override
    public String convertToDatabaseValue(List<ProcessNewBean.ServiceBaseShortBean> entityProperty) {
        if (entityProperty == null) {
            return null;
        } else {
            return new Gson().toJson(entityProperty);
        }
    }
}
